﻿package com.oyjd.ybx.front.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestUtils;

import com.oyjd.ybx.bean.vo.CommentVO;
import com.oyjd.ybx.bean.vo.UserVO;
import com.oyjd.ybx.business.comment.service.CommentService;
import com.oyjd.ybx.fw.action.BaseAction;
import com.oyjd.ybx.fw.action.Page;
import com.oyjd.ybx.fw.util.CodeUtil;
import com.oyjd.ybx.fw.util.OutUtil;

/**
 * 
 * 评论信息Action控制层
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Action(value = "/frontCmt")
public class FrontCmtAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 23354347027015039L;

	private static final Log log = LogFactory.getLog(FrontCmtAction.class);
	private CommentService service;

	@Autowired
	public void setService(CommentService service) {
		this.service = service;
	}

	/**
	 * 获取评论信息列表数据
	 * 
	 * @return
	 */
	public String getList() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			Page<CommentVO> page = service.getList(new Page<CommentVO>(req));
			//
			map.put("count", page.getCount());
			map.put("info", page.getResult());
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 新增评论信息
	 * 
	 * @return
	 */
	public String add() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			String goodsId = ServletRequestUtils.getStringParameter(req, "goodsId");
			String commentInfo = ServletRequestUtils.getStringParameter(req, "commentInfo");
			UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);
			if (null != userVO) {
				service.add(goodsId, userVO.getAccount(), commentInfo, req.getRemoteAddr());
				map.put("code", CodeUtil.SUCCESS);
			} else {
				map.put("code", CodeUtil.NO_LOGIN);
			}
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

}
